<script>
import { mapGetters } from 'vuex'
import AreaForm from '@/views/car/area/components/areaForm.vue'

export default {
  name: 'area',
  components: { AreaForm },
  data() {
    return {
      /* 获取区域列表的分页参数 */
      pageParams: {
        page: 1,
        pageSize: 10
      },
      /* 加载区域列表 */
      loading: false,
      /* 是否显示添加或者编辑区域的弹框 */
      dialogVisible: false,
      /* 添加状态还是编辑状态 */
      edit: false
    }
  },
  methods: {
    /* 获取区域列表的回调函数 */
    async fetchAreaList(pageParams) {
      try {
        this.loading = true
        await this.$store.dispatch('areaStore/fetchAreaList', pageParams)
      } catch (error) {
        this.$message.error(error.message)
      } finally {
        this.loading = false
      }
    },
    /* 分页器size发生变化的回调函数 */
    handleSizeChange(pageSize) {
      this.pageParams.pageSize = pageSize
      this.fetchAreaList(this.pageParams)
    },
    /* 分页器page发生变化的回调函数 */
    handleCurrentChange(page) {
      this.pageParams.page = page
      this.fetchAreaList(this.pageParams)
    },
    /* 点击删除区域的回调函数 */
    async handleDelete(row) {
      const confirm = await this.$confirm(`删除${row.areaName}?是否继续`, '提示').catch(error => error)
      if (confirm === 'cancel') return
      try {
        const deleteResult = await this.$store.dispatch('areaStore/deleteArea', row.id)
        this.fetchAreaList(this.pageParams)
        this.$message.success(deleteResult)
      } catch (error) {
        this.$message.error(error.message)
      }
    },
    /* 点击添加区域的回调函数 */
    handleAdd() {
      this.edit = false
      this.dialogVisible = true
    },
    /* 点击编辑区域的回调函数 */
    handleEdit(row) {
      this.edit = true
      this.dialogVisible = true
      this.$nextTick(()=>{
        this.$refs.areaForm.findArea(row)
      })
    },
    /* area-form组件添加获取编辑区域成功的自定义事件 */
    handleSuccess() {
      this.dialogVisible = false
      this.fetchAreaList(this.pageParams)
    }
  },
  computed: {
    ...mapGetters('areaStore', ['areaList', 'total']),
    dialogTitle() {
      return this.edit ? '编辑区域' : '添加区域'
    }
  },
  mounted() {
    this.fetchAreaList(this.pageParams)
  }
}
</script>

<template>
  <div class="area">
    <el-card shadow="never">
      <el-button type="primary" size="small" icon="el-icon-plus" @click="handleAdd">添加区域</el-button>
    </el-card>
    <el-card shadow="never">
      <el-table border stripe size="small" :header-cell-style="{backgroundColor:'#f4f6f8'}" :data="areaList" v-loading="loading" element-loading-text="拼命加载中">
        <el-table-column label="序号" type="index" align="center"></el-table-column>
        <el-table-column label="区域名称" prop="areaName" align="center"></el-table-column>
        <el-table-column label="车位数（个）" prop="spaceNumber" align="center"></el-table-column>
        <el-table-column label="面积（㎡）" prop="areaProportion" align="center"></el-table-column>
        <el-table-column label="计费规则" prop="ruleName" align="center"></el-table-column>
        <el-table-column label="规则详情" prop="hoverRuleName" align="center" show-overflow-tooltip></el-table-column>
        <el-table-column label="备注" prop="remark" align="center"></el-table-column>
        <el-table-column label="操作" align="center">
          <template #default="{row}">
            <el-button type="warning" size="small" icon="el-icon-edit" @click="handleEdit(row)">编辑区域</el-button>
            <el-button type="danger" size="small" icon="el-icon-delete" @click="handleDelete(row)">删除区域</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageParams.page" :page-sizes="[10, 20, 30, 40]" :page-size="pageParams.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total" background></el-pagination>
    </el-card>

    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="35%" @close="$refs.areaForm.resetFormData()">
      <area-form @success="handleSuccess" ref="areaForm"></area-form>
    </el-dialog>
  </div>
</template>

<style scoped lang="scss">
.area {
  padding: 10px 0;

  .el-card {
    margin-bottom: 10px;
  }

  .el-table {
    margin: 10px 0;
  }

  .el-pagination {
    display: flex;
    justify-content: center;
  }
}
</style>
